package com.powervision.mvvmdemo.ui.activity

import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import by.kirich1409.viewbindingdelegate.viewBinding
import com.aisier.architecture.util.startActivity
import com.powervision.base.base.BaseActivity
import com.powervision.base.util.LogUtils
import com.powervision.mvvmdemo.R
import com.powervision.mvvmdemo.databinding.ActivityRegisterBinding
import com.powervision.mvvmdemo.ui.viewmodel.RegisterViewModel

/**
 * @auth: njb
 * @date: 2021/12/13 15:50
 * @desc: 描述
 */
class RegisterActivity : BaseActivity(R.layout.activity_register) {
    private val mBinding by viewBinding(ActivityRegisterBinding::bind)

    private val mViewModel by viewModels<RegisterViewModel>()

    private var userName: String? = ""
    private var password: String? = ""
    private var repassword: String? = ""

    override fun init() {
        initView()
        initData()
    }

    private fun initData() {

        mViewModel.userLiveData.observeState(this) {
            onSuccess {
                LogUtils.d("--userInfo--", it.toString())
                finish()
                startActivity<LoginActivity>()
            }
            onException { LogUtils.e("--error--", it.message.toString()) }
        }
    }

    private fun initView() {
        mBinding.etPassWord.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
                userName = s.toString().trim()
            }

            override fun afterTextChanged(p0: Editable?) {
            }

        })
        mBinding.etRepassWord.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
                password = s.toString().trim()

            }

            override fun afterTextChanged(p0: Editable?) {
            }

        })

        mBinding.etRepassWord.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
                repassword = s.toString().trim()

            }

            override fun afterTextChanged(p0: Editable?) {
            }

        })

        mBinding.btnRegister.setOnClickListener {
            mViewModel.register(userName!!, password!!, repassword!!)
        }
    }
}

